/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.common.util;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:ListUtil <br/>
 * Date: 2019年2月27日 下午3:26:48 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
public class ListUtil {

    /**
     * 将List中的数据随机排列，不会减少数据，只是将存放数据的顺序打乱
     * 
     * @param list
     * @return
     */
    public static <T> List<T> randomSort(List<T> list) {
        if (list == null || list.size() <= 1) {
            return list;
        }
        List<T> resultList = new ArrayList<T>();

        List<T> newList = new ArrayList<T>();
        for (int i = 0; i < list.size(); i++) {
            newList.add(list.get(i));
        }
        while (newList.size() > 0) {
            int random = (int) (Math.random() * newList.size());
            for (int i = 0; i < newList.size(); i++) {
                if (i == random) {
                    resultList.add(newList.get(i));
                    newList.remove(i);
                }
            }
        }
        return resultList;

    }
}
